package com.sgb.kjwl.view.ui.adapter.self.carMasters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.BargainerBargainingEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.carMaster.BargainerBargainingActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BargainerBargainingAdapter extends BaseRecycleAdapter<BargainerBargainingEntity> {

    private TextView agree;
    private TextView tv_oder;
    private TextView tv_oder_no;
    private IButton iButton;

    public BargainerBargainingAdapter(Context context, List<BargainerBargainingEntity> datas) {
        super(context, R.layout.item_bargainer_bargaining, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final BargainerBargainingEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        agree = holder.itemView.findViewById(R.id.tv_agree);
        tv_oder = holder.itemView.findViewById(R.id.tv_oder);
        tv_oder_no = holder.itemView.findViewById(R.id.tv_oder_no);
        TextView tvDetail=holder.itemView.findViewById(R.id.tv_detail);
        if (entity.getOrderNo().equals("")) {
            agree.setVisibility(View.VISIBLE);
            tv_oder.setVisibility(View.GONE);
            tv_oder_no.setVisibility(View.GONE);
            tvDetail.setVisibility(View.VISIBLE);
        } else {
            agree.setVisibility(View.GONE);
            tv_oder.setVisibility(View.VISIBLE);
            tv_oder_no.setVisibility(View.VISIBLE);
            tvDetail.setVisibility(View.GONE);
        }
        agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iButton.agree(entity.getId() + "");
            }
        });
        tv_oder_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getH5Url(entity.getOrderNo());

            }
        });
        tvDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getH5Url2(entity.getUserNo(),entity.getCarrierNo());

            }
        });
        holder.itemView.findViewById(R.id.ll_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getH5Url3(entity.getCarrierNo());
            }
        });
    }

    public interface IButton {
        void agree(String id);
    }

    public void OnButtonClick(IButton iButton) {
        this.iButton = iButton;
    }
    public void getH5Url3(String id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("carrierDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id+"&areaCode="+((BargainerBargainingActivity)context).viewModel.repertory.getAreaCode()+"&isMyMenu=true";
                    Log.e("madong_test","4====="+url);
                    WebViewX5HostActivity.start(context, url);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    // 详情       车主订单详情 参数  goodsOrderDetails      货源详情参数   sourceDetails     ownerDetails
    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("ownerOrderDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    Log.e("madong_test","4====="+url);
                    WebViewX5HostActivity.start(context, url);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getH5Url2(String userNo,String carrierNo) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("bargainDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?userNo=" + userNo+"&carrierNo="+carrierNo;
                    Log.e("madong_test","4====="+url);
                    WebViewX5HostActivity.start(context, url);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
